package org.future.wsdl.data;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;

import com.ibm.wsdl.extensions.http.HTTPConstants;
import com.ibm.wsdl.extensions.mime.MIMEConstants;
import com.ibm.wsdl.extensions.soap.SOAPConstants;
import com.ibm.wsdl.extensions.soap12.SOAP12Constants;

/**
 * wsdl:binding 中的  wsdl:operation 元素
 * @author dycc
 *
 */
public class WsdlBindingOperation {
    private String name;
    
    private QName extType;
    
    private String soapAction;
    
    private String style;
    
    private String location;
    
    private QName inputExtName;
    // for soap & soap12 binding
    private String inputUse;
    // for http binding
    private String inputType;
    // for http binding
    private String inputPart;
    
    private QName outputExtName;
    // for soap & soap12 binding
    private String outputUse;
    // for http binding
    private String outputType;
    // for http binding
    private String outputPart;
    // faults
    private List<WsdlBindingFault> bindingFaults = new ArrayList<WsdlBindingFault>();
    
    // name
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    // extType
    public QName getExtType() {
        return extType;
    }
    public void setExtType(QName extType) {
        this.extType = extType;
    }
    // soapAction
    public String getSoapAction() {
        return soapAction;
    }
    public void setSoapAction(String soapAction) {
        this.soapAction = soapAction;
    }
    // style
    public String getStyle() {
        return style;
    }
    public void setStyle(String style) {
        this.style = style;
    }
    // location
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    // inputExtName
    public QName getInputExtName() {
        return inputExtName;
    }
    public void setInputExtName(QName inputExtName) {
        this.inputExtName = inputExtName;
    }
    // inputUse
    public String getInputUse() {
        return inputUse;
    }
    public void setInputUse(String inputUse) {
        this.inputUse = inputUse;
    }
    // inputType
    public String getInputType() {
        return inputType;
    }
    public void setInputType(String inputType) {
        this.inputType = inputType;
    }
    // inputPart
    public String getInputPart() {
        return inputPart;
    }
    public void setInputPart(String inputPart) {
        this.inputPart = inputPart;
    }
    // outputExtName
    public QName getOutputExtName() {
        return outputExtName;
    }
    public void setOutputExtName(QName outputExtName) {
        this.outputExtName = outputExtName;
    }
    // outputUse
    public String getOutputUse() {
        return outputUse;
    }
    public void setOutputUse(String outputUse) {
        this.outputUse = outputUse;
    }
    // outputType
    public String getOutputType() {
        return outputType;
    }
    public void setOutputPart(String outputPart) {
        this.outputPart = outputPart;
    }
    // outputPart
    public String getOutputPart() {
        return outputPart;
    }
    public void setOutputType(String outputType) {
        this.outputType = outputType;
    }
    // bindingFaults
    public List<WsdlBindingFault> getBindingFaults() {
        return bindingFaults;
    }
    public void setBindingFaults(List<WsdlBindingFault> bindingFaults) {
        this.bindingFaults = bindingFaults;
    }
    
    // toString
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append("<wsdl:operation name=\"" + name + "\" >");
        sb.append("\n");
        sb.append("<" + extType.getPrefix() + ":" + extType.getLocalPart());
        if(extType.equals(SOAP12Constants.Q_ELEM_SOAP_OPERATION)
                || extType.equals(SOAPConstants.Q_ELEM_SOAP_OPERATION)){
            sb.append(" soapAction=\"" + soapAction + "\"");
            sb.append(" style=\"" + style +"\"");
        }else if(extType.equals(HTTPConstants.Q_ELEM_HTTP_OPERATION)){
            sb.append(" location=\"" + location + "\"");
        }
        sb.append(" />");
        // add wsdl:input
        if(inputExtName != null){
            sb.append("\n");
            sb.append("<wsdl:input>");
            sb.append("\n");
            sb.append("<" + inputExtName.getPrefix() + ":" + inputExtName.getLocalPart());
            if(inputExtName.equals(SOAPConstants.Q_ELEM_SOAP_BODY) 
                    ||inputExtName.equals(SOAP12Constants.Q_ELEM_SOAP_BODY)){
                sb.append(" use=\"" + inputUse + "\" ");
            }else if(inputExtName.equals(MIMEConstants.Q_ELEM_MIME_CONTENT)){
                sb.append(" type=\"" + inputType + "\" part=\"" + inputPart + "\" ");
            }
            sb.append("/>");
            sb.append("\n");
            sb.append("</wsdl:input>");
        }
        // add wsdl:output
        if(outputExtName != null){
            sb.append("\n");
            sb.append("<wsdl:output>");
            sb.append("\n");
            sb.append("<" + outputExtName.getPrefix() + ":" + outputExtName.getLocalPart());
            if(outputExtName.equals(SOAPConstants.Q_ELEM_SOAP_BODY) 
                    ||outputExtName.equals(SOAP12Constants.Q_ELEM_SOAP_BODY)){
                sb.append(" use=\"" + outputUse + "\" ");
            }else if(outputExtName.equals(MIMEConstants.Q_ELEM_MIME_CONTENT)){
                sb.append(" type=\"" + outputType + "\" part=\"" + outputPart + "\" ");
            }
            sb.append("/>");
            sb.append("\n");
            sb.append("</wsdl:output>");
        }
        // add wsdl:fault
        sb.append("\n");
        for(int i=0;i<bindingFaults.size();i++){
            sb.append(bindingFaults.get(i));
            sb.append("\n");
        }
        sb.append("</wsdl:operation>");
        return sb.toString();
    }
}
